package linkedList;

public class LinkedListStack <E> {

	/**
	 * 是用链表实现栈(先进后出)
	 */
	private LinkedList<E> linkedList;
	private int size;
	
	/**
	 * 构造函数
	 */
	public LinkedListStack() {
		size = 0;
		linkedList = new LinkedList<E>();
	}
	
	public void push(E e) {
		linkedList.addFirst(e);
		size ++;
	}
	
	public E pop() {
		size --;
		return linkedList.removeFirst();
	}
	
	public E peek() {
		return linkedList.getFirst();
	}
	
	public int getSize() {
		return size;
	}
	
	public boolean isEmpty() {
		return size == 0;
	}
	
	@Override
	public String toString() {
		StringBuilder res = new StringBuilder();
		res.append("LinkedListStack,size:"+size);
		res.append("[");
		res.append(linkedList.toString());
		res.append("]");
		return res.toString();
	}
	
	public static void main(String[] args) {
		LinkedListStack<Integer> linkedStack = new LinkedListStack<>();
		for(int i = 0 ; i < 5 ; i++) {
			linkedStack.push(i);
			System.out.println(linkedStack);
		}
		linkedStack.pop();
		System.out.println(linkedStack);
		System.out.println(linkedStack.peek());
		
	}
}
